<?php

namespace App\Model;

use Illuminate\Database\Eloquent\Model;

/**
 * 用户拉黑
 */
class UserBlackList extends Model
{
    protected $table = 'user_blacklist';

    protected $casts = [
        'created_at' => 'datetime:Y-m-d H:i:s',
        'updated_at' => 'datetime:Y-m-d H:i:s'
    ];

    /**
     * 查找拉黑记录
     */
    public static function check($userId = 0, $blackUserId = 0)
    {
        $where = ['user_id' => $userId, 'black_user_id' => $blackUserId, 'flag' => 1];
        return self::where($where)->first();
    }

    /**
     * 拉黑
     */
    public static function removeRecord($userId = 0, $blackUserId = 0)
    {
        $record = self::check($userId, $blackUserId);
        if (!$record) return false;
        $record->flag = 0;
        return $record->saveOrFail() ? 1 : 0;
    }
}
